home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 12 code / Components / Sources / MathComponentCommon.c < prev    next >
Encoding:
Text File  |  1994-12-02  |  3.6 KB  |  163 lines  |  [TEXT/MMCC]

  1. /*
  2.     File:        MathComponentCommon.c
  3.  
  4.     Contains:    Math component routines.
  5.  
  6.     Written by:    Gary Woodcock
  7.  
  8.     Copyright:    © 1992 by Apple Computer, Inc.
  9.  
  10.     Change History (most recent first):
  11.  
  12. */
  13.  
  14. //-----------------------------------------------------------------------
  15. // Includes
  16.  
  17. #include "MathComponent.h"
  18. #include "MathComponentCommon.h"
  19. #include "MathComponentPrivate.h"
  20. #include <OSUtils.h>
  21.  
  22.  
  23. //-----------------------------------------------------------------------
  24.  
  25. pascal    ComponentResult    MathOpen    (ComponentInstance    self)
  26. {
  27.     
  28.     ComponentResult        result = noErr;
  29.     PrivateGlobals**    globals;
  30.     short                count;
  31.     
  32.     // Can we open another instance?
  33.     count = CountComponentInstances ((Component) self);
  34.     if (count <= kMaxMathInstances)
  35.     {
  36.         // Did we get our storage?
  37.         globals = (PrivateGlobals**) NewHandleClear (sizeof (PrivateGlobals));
  38.         if (globals != nil)
  39.         {
  40.             // Keep a reference to self
  41.             (*globals)->self = (Component) self;
  42.             
  43.             // Init private fields
  44.             (*globals)->capturingComponentInstance = 0L;
  45.             (*globals)->delegateComponent = 0L;
  46.             (*globals)->delegateComponentInstance = 0L;
  47.             // Set storage ref
  48.             SetComponentInstanceStorage (self, (Handle) globals);
  49.         }
  50.         else    // NewHandleClear failed
  51.         {
  52.             result = MemError();
  53.         }
  54.     }
  55.     else    // No more instances can be opened
  56.     {
  57.         result = kGenericError;
  58.     }
  59.     return (result);
  60. }
  61.  
  62. //-----------------------------------------------------------------------
  63.  
  64. pascal    ComponentResult    MathClose    (Handle                storage,
  65.                                      ComponentInstance    self)
  66. {
  67.     ComponentResult        result = noErr;
  68.     PrivateGlobals**    globals = (PrivateGlobals**) storage;
  69.  
  70.     // Do we have any clean up to do?
  71.     if (globals != nil)
  72.     {
  73.         // Dispose globals
  74.         DisposeHandle ((Handle) globals);
  75.         globals = nil;
  76.  
  77.     }
  78.     return (result);
  79. }
  80.  
  81. //-----------------------------------------------------------------------
  82. pascal    ComponentResult    MathCanDo    (short    selector)
  83. {
  84.     // Case on the request code
  85.     switch (selector)
  86.     {
  87.         // Component Manager request codes
  88.         case kComponentOpenSelect:
  89.         case kComponentCloseSelect:
  90.         case kComponentCanDoSelect:
  91.         case kComponentVersionSelect:
  92.         case kComponentTargetSelect:
  93.         
  94.         // Math component request codes
  95.         case kDoDivideSelect:
  96.         case kDoMultiplySelect:
  97.         {
  98.             return (true);
  99.         }
  100.         case kComponentRegisterSelect:    // Register request not supported
  101.         default:                        // Not a request code we recognize
  102.         {
  103.             return (false);
  104.         }
  105.     }
  106. }
  107.  
  108. //-----------------------------------------------------------------------
  109.  
  110. pascal    ComponentResult    MathVersion    (void)
  111. {
  112.     // Return the version info
  113.  
  114.     return (mathInterfaceRevision);
  115. }
  116.  
  117. //-----------------------------------------------------------------------
  118.  
  119. pascal    ComponentResult    MathTarget    (Handle                storage,
  120.                                      ComponentInstance    capturingComponent)
  121. {
  122.     ComponentResult        result = noErr;
  123.     PrivateGlobals**    globals = (PrivateGlobals**) storage;
  124.     
  125.     // Set reference to capturing component
  126.     (*globals)->capturingComponentInstance = capturingComponent;
  127.     
  128.     return (result);
  129. }
  130.  
  131. //-----------------------------------------------------------------------
  132.  
  133. pascal    ComponentResult    MathDoDivide    (short    numerator,
  134.                                          short    denominator,
  135.                                          short    *quotient)
  136. {
  137.     ComponentResult    result = noErr;
  138.     if (denominator != 0)
  139.     {
  140.         *quotient = numerator/denominator;
  141.     }
  142.     else    // Divide by zero not allowed
  143.     {
  144.         *quotient = 0;
  145.         result = kGenericError;    
  146.     }
  147.     return (result);
  148. }
  149.  
  150. //-----------------------------------------------------------------------
  151.  
  152. pascal    ComponentResult    MathDoMultiply    (short    firstNum,
  153.                                          short    secondNum,
  154.                                          short    *multiplicationResult)
  155. {
  156.     ComponentResult    result = noErr;
  157.     *multiplicationResult = firstNum * secondNum;
  158.     
  159.     return (result);
  160. }
  161.     
  162. //-----------------------------------------------------------------------
  163.